const axios = require('axios')
const {api, key} = require("./config.json")

class Google {
    static getSupportedLanguages(){
        return new Promise((resolve, reject) => {
            axios.get(`${api}/language/translate/v2/languages?key=${key}`).then(res => {
                if(res.data && res.data.data){
                    resolve(res.data)
                }else{
                    reject(res.data)
                }
            })
        })
    }

    static translate(q, target = 'zh-tw', source = "", format = "text"){
        console.log(...arguments)
        return new Promise((resolve, reject) => {
            axios.post(`${api}/language/translate/v2`,null, {params: {
                q,
                target,
                source,
                format,
                key
            }}).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err, 'err')
            })
        })
    }
}

module.exports = Google


Google.translate('hello').then(res => {
    console.log(res.data, 'data')
}).catch(err => {
    console.log(err, 'err')
})